home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 3 of 3 / CHAPTER8 / GINSTDRV.C < prev    next >
C/C++ Source or Header  |  1996-04-28  |  8KB  |  256 lines

  1.  
  2. #include <windows.h>  
  3. #include <stdio.h>
  4. #include "ginstdrv.h"
  5. #include "sqlext.h"  
  6. #include "odbcinst.h"  
  7.  
  8.  
  9. #if defined (WIN32)
  10.     #define IS_WIN32 TRUE
  11. #else
  12.     #define IS_WIN32 FALSE
  13. #endif
  14.  
  15. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  16. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  17. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  18.  
  19. HINSTANCE hInst;   // current instance
  20.  
  21. LPCTSTR lpszAppName = "MyApp";
  22. LPCTSTR lpszTitle   = "SQLGetInstalledDrivers()"; 
  23.  
  24.  
  25. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  26. VOID DisplayBuffer( HWND hList, LPCSTR lpszBuffer );
  27.  
  28.  
  29. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  30.                       LPTSTR lpCmdLine, int nCmdShow)
  31. {
  32.    MSG      msg;
  33.    HWND     hWnd; 
  34.    WNDCLASS wc;
  35.  
  36.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  37.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  38.    wc.cbClsExtra    = 0;                      
  39.    wc.cbWndExtra    = 0;                      
  40.    wc.hInstance     = hInstance;              
  41.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  42.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  43.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  44.    wc.lpszMenuName  = lpszAppName;              
  45.    wc.lpszClassName = lpszAppName;              
  46.  
  47.    if ( IS_WIN95 )
  48.    {
  49.       if ( !RegisterWin95( &wc ) )
  50.          return( FALSE );
  51.    }
  52.    else if ( !RegisterClass( &wc ) )
  53.       return( FALSE );
  54.  
  55.    hInst = hInstance; 
  56.  
  57.    hWnd = CreateWindow( lpszAppName, 
  58.                         lpszTitle,    
  59.                         WS_OVERLAPPEDWINDOW, 
  60.                         CW_USEDEFAULT, 0, 
  61.                         CW_USEDEFAULT, 0,  
  62.                         NULL,              
  63.                         NULL,              
  64.                         hInstance,         
  65.                         NULL               
  66.                       );
  67.  
  68.    if ( !hWnd ) 
  69.       return( FALSE );
  70.  
  71.    ShowWindow( hWnd, nCmdShow ); 
  72.    UpdateWindow( hWnd );         
  73.  
  74.    while( GetMessage( &msg, NULL, 0, 0) )   
  75.    {
  76.       TranslateMessage( &msg ); 
  77.       DispatchMessage( &msg );  
  78.    }
  79.  
  80.    return( msg.wParam ); 
  81. }
  82.  
  83.  
  84. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  85. {
  86.    WNDCLASSEX wcex;
  87.  
  88.    wcex.style         = lpwc->style;
  89.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  90.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  91.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  92.    wcex.hInstance     = lpwc->hInstance;
  93.    wcex.hIcon         = lpwc->hIcon;
  94.    wcex.hCursor       = lpwc->hCursor;
  95.    wcex.hbrBackground = lpwc->hbrBackground;
  96.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  97.    wcex.lpszClassName = lpwc->lpszClassName;
  98.  
  99.    // Added elements for Windows 95.
  100.    //...............................
  101.    wcex.cbSize = sizeof(WNDCLASSEX);
  102.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  103.                             IMAGE_ICON, 16, 16,
  104.                             LR_DEFAULTCOLOR );
  105.             
  106.    return RegisterClassEx( &wcex );  
  107. }
  108.  
  109.  
  110. #define BUFFERSIZE 1024
  111.  
  112. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  113. {
  114. static HWND hList = NULL;
  115.  
  116.    switch( uMsg )
  117.    {
  118.       case WM_CREATE :
  119.               {
  120.                  hList = CreateWindow( "LISTBOX", "",    
  121.                                        LBS_NOTIFY | WS_VSCROLL | 
  122.                                        WS_BORDER  | WS_CHILD | 
  123.                                        WS_VISIBLE | LBS_NOINTEGRALHEIGHT, 
  124.                                        0, 0, 
  125.                                        0, 0,  
  126.                                        hWnd,              
  127.                                        (HMENU)101,              
  128.                                        hInst,         
  129.                                        NULL               
  130.                                      );
  131.               }
  132.               break;
  133.  
  134.       case WM_SIZE :
  135.               MoveWindow( hList, 0, 0, LOWORD( lParam ), 
  136.                                        HIWORD( lParam ), TRUE );
  137.               break; 
  138.               
  139.       case WM_COMMAND :
  140.               switch( LOWORD( wParam ) )
  141.               {
  142.                  case IDM_TEST :
  143.                         {
  144.                            BOOL  bRet;
  145.                            UCHAR szLBStr[128];
  146.                            UCHAR szBuf[BUFFERSIZE];
  147.                            WORD  cbBufOut;   // WORD matches fn prototype
  148.  
  149.                            // Call SQLGetInstalledDrivers() to
  150.                            // retrieve a list of installed drivers.
  151.                            // .....................................
  152.                            bRet = SQLGetInstalledDrivers( szBuf, 
  153.                                      BUFFERSIZE, &cbBufOut );
  154.  
  155.                            strcpy( szLBStr, "SQLGetInstalledDrivers() " );
  156.  
  157.                            // Display status to the user.
  158.                            // ...........................
  159.                            if( bRet )
  160.                            {
  161.                               strcat( szLBStr, "successful" );
  162.  
  163.                               SendMessage( hList, LB_ADDSTRING, 0, 
  164.                                           (LPARAM)szLBStr );
  165.  
  166.                               // Display retrieved driver 
  167.                               // names to the user.
  168.                               // ........................
  169.                               DisplayBuffer( hList, szBuf );
  170.                            }
  171.                            else
  172.                            {
  173.                               strcat( szLBStr, "not successful" );
  174.  
  175.                               SendMessage( hList, LB_ADDSTRING, 0, 
  176.                                           (LPARAM)szLBStr );
  177.                            }
  178.                         }
  179.                         break;
  180.  
  181.                  case IDM_ABOUT :
  182.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  183.                         break;
  184.  
  185.                  case IDM_EXIT :
  186.                         DestroyWindow( hWnd );
  187.                         break;
  188.               }
  189.               break;
  190.       
  191.       case WM_DESTROY :
  192.               PostQuitMessage(0);
  193.               break;
  194.  
  195.       default :
  196.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  197.    }
  198.  
  199.    return( 0L );
  200. }
  201.  
  202.  
  203. // Parse the returned buffer and send
  204. // driver names to the listbox.
  205. // ..................................
  206. VOID DisplayBuffer( HWND hList, LPCSTR lpszBuffer )
  207. {
  208.    char* p;    // string traversal pointer.
  209.  
  210.    p = (char*)lpszBuffer;
  211.  
  212.    while( *p )
  213.    {
  214.       SendMessage( hList, LB_ADDSTRING, 0, (LPARAM)p );
  215.  
  216.       // Traverse to the end of the string.
  217.       // ..................................
  218.       do
  219.       {
  220.          p++;
  221.       }
  222.       while( *p );
  223.  
  224.       // Traverse to the beginning of the next string. 
  225.       // The outer loop will terminate when we arrive
  226.       // at to the end of the buffer, since the end of 
  227.       // the buffer contains two null characters.
  228.       // .............................................
  229.       p++;
  230.    }
  231. }
  232.  
  233.  
  234. LRESULT CALLBACK About( HWND hDlg,           
  235.                         UINT message,        
  236.                         WPARAM wParam,       
  237.                         LPARAM lParam)
  238. {
  239.    switch (message) 
  240.    {
  241.        case WM_INITDIALOG: 
  242.                return (TRUE);
  243.  
  244.        case WM_COMMAND:                              
  245.                if (   LOWORD(wParam) == IDOK         
  246.                    || LOWORD(wParam) == IDCANCEL)    
  247.                {
  248.                        EndDialog(hDlg, TRUE);        
  249.                        return (TRUE);
  250.                }
  251.                break;
  252.    }
  253.  
  254.    return (FALSE); 
  255. }
  256.